<?php
namespace app\controllers;


use app\common\corelib\LocalUpload;
use app\logic\BannerLogic;
use yii;
//轮播图管理
class BannerController extends BackendController
{
    //轮播图管理
    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $bannerLogic = new BannerLogic();
        $this->data['breadcrumbs'] = '轮播图管理';
        return $this->render('index', [
            'banner' => $bannerLogic->getBanner($query, $page),
            'query' => $query
        ]);
    }
    //添加轮播图
    public function actionAdd(){
        $bannerLogic = new BannerLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['轮播图管理' => U('banner/index'), ' 添加轮播图' => ''];
            return $this->render('add', [
                'theme' => '/assets/',
                'actionUrl' => U('banner/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            if($_FILES['banner_pic']['name']){
                LocalUpload::go_upload($_FILES['banner_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $bannerLogic->addBanner($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增轮播图成功');
                jump('banner/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增轮播图失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //编辑轮播图
    public function actionEdit(){
        $bannerLogic = new BannerLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('banner_id');
            $banner = $bannerLogic->getById($id);
            $this->data['breadcrumbs'] = ['轮播图管理' => U('banner/index'), $banner['banner_title'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$banner['banner_pic'])){
                $banner_pic = $banner['banner_pic'];
            }else{
                $banner_pic = '/default.jpg';
            }
            return $this->render('edit', [
                'banner' => $banner,
                'banner_pic' => $banner_pic,
                'theme' => '/assets/',
                'actionUrl' => U('banner/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('banner_id');
            if($_FILES['banner_pic']['name']){
                LocalUpload::go_upload($_FILES['banner_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $bannerLogic->editById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新轮播图成功');
                jump('banner/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新轮播图失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //删除轮播图
    public function actionDelete(){
        $id = Yii::$app->request->get('banner_id');
        $bannerLogic = new BannerLogic();
        $logicRes = $bannerLogic->delete($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        return $this->redirect(U('banner/index'));
    }


}